<template>
  <div class="chart">
    <el-card>
      <ul class="top">
        <li>
          <div class="top-num">{{ form.increment_users }}</div>
          <span class="text">今日新增用户</span>
        </li>
        <li>
          <div class="top-num">{{ form.total_users }}</div>
          <span>总用户量</span>
        </li>
        <li>
          <div class="top-num">{{ form.increment_users }}</div>
          <span>新增试题</span>
        </li>
        <li>
          <div class="top-num">{{ form.total_questions }}</div>
          <span>总试题量</span>
        </li>
        <li>
          <div class="top-num">{{ form.total_done_questions }}</div>
          <span>总刷题量</span>
        </li>
        <li>
          <div class="top-num">{{ form.personal_questions }}</div>
          <span>人均刷题量</span>
        </li>
      </ul>
    </el-card>
    <el-card>
      <div style="width: 1000px; height: 450px"
           ref="chart"></div>
    </el-card>
  </div>
</template>

<script>
import { dataTitle, dataStatistics } from '@/api/chart.js'
import * as echarts from 'echarts'

export default {
  data () {
    return {
      form: {
        total_done_questions: '',
        personal_questions: '',
        total_users: '',
        increment_users: '',
        increment_questions: '',
        total_questions: ''
      },
      info: '',


    }
  },
  created () {
    this.getData()
  },
  mounted () {
    this.initData()
  },

  methods: {
    async getData () {
      const res = await dataTitle()
      console.log('获取顶部数据', res)
      this.form = res.data
    },
    async initData () {
      const res = await dataStatistics()
      console.log('数据信息', res)
      this.info = res.data

      const arrNew = this.info.map(item => {
        return item.name;
      })

      console.log(arrNew);
      const myChart = echarts.init(this.$refs.chart)

      const option = {
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)',


        },
        legend: {
          top: '5%',
          right: 10,
          data: arrNew
        },
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: '26',
                fontWeight: 'bold',
                color: 'red'
              }
            },
            labelLine: {
              show: false
            },
            data: this.info

          }
        ]
      }

      option && myChart.setOption(option)
    }
  }
}
</script>

<style lang="less" scoped>
.chart {
  .top {
    display: flex;
    align-items: center;
    justify-content: space-around;
    list-style-type: none;
    text-align: center;
  }
  .top-num {
    width: 90px;
    height: 90px;
    border: 3px solid #008000;
    border-radius: 50%;
    line-height: 90px;
  }
}
</style>
